import 'dart:core';
import 'dart:io';

import 'package:FineArt/utils/log_util.dart';
import 'package:FineArt/utils/size_config.dart';
import 'package:flutter/material.dart';
import 'package:share/share.dart';
import 'package:webview_flutter/webview_flutter.dart';

class WebViewPageNew extends StatefulWidget {
  final String url;
  final String title;
  WebViewController _webViewController;

  WebViewPageNew(this.url, this.title);

  _WebViewPageState1 createState() => _WebViewPageState1();
}

class _WebViewPageState1 extends State<WebViewPageNew> {
  final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();

  JavascriptChannel jsBridge(BuildContext context) => JavascriptChannel(
      name: 'jsbridge', // 与h5 端的一致 不然收不到消息
      onMessageReceived: (JavascriptMessage message) async {
        debugPrint(message.message);
      });

  @override
  void initState() {
    super.initState();
    LogUtil.d("initState webview build url:" + widget.url);
    // Enable hybrid composition.
    // if (Platform.isAndroid)
    //   WebView.platform = SurfaceAndroidWebView();
  }

  @override
  Widget build(BuildContext context) {
    LogUtil.d("webview build url:" + widget.url);
    return Scaffold(appBar: _buildAppbar(), body: _buildBody());
  }

  _buildAppbar() {
    return AppBar(
        elevation: 0,
        backgroundColor: Colors.white,
        brightness: Brightness.light,
        title: Text(
          widget.title,
          style: TextStyle(color: Colors.black),
        ),
        centerTitle: true,
        leading: IconButton(
            icon: Image.asset(
              "assets/images/icon_back_black.png",
              width: getPortraitSWidth(11),
              height: getPortraitSHeight(18),
            ),
            onPressed: () {
              Navigator.of(context).pop();
            }));
  }

  _buildBody() {
    return Column(
      children: <Widget>[
        SizedBox(
          height: 1,
          width: double.infinity,
          child: const DecoratedBox(
              decoration: BoxDecoration(color: Color(0xFFEEEEEE))),
        ),
        Expanded(
          flex: 1,
          child: WebView(
            initialUrl: widget.url,
            javascriptMode: JavascriptMode.unrestricted,
            javascriptChannels: <JavascriptChannel>[jsBridge(context)].toSet(),
            onWebViewCreated: (WebViewController controller) {
              LogUtil.d("onWebViewCreated >> $controller");
              widget._webViewController = controller;

              controller.loadUrl(widget.url);

              controller
                  .canGoBack()
                  .then((value) => debugPrint(value.toString()));
              controller
                  .canGoForward()
                  .then((value) => debugPrint(value.toString()));
              controller.currentUrl().then((value) => debugPrint(value));
            },
            onPageFinished: (String value) {
              // widget._webViewController.evaluateJavascript('document.title')
              //     .then((title) => debugPrint(title));
            },
          ),
        )
      ],
    );
  }
}
